import { spawnSync } from 'child_process'
import { consola as logger } from 'consola'
import { readFileSync, existsSync } from 'fs';

export default async (path: string) => {
  const root = process.cwd();
  try {
    let repoMapping = JSON.parse(readFileSync(`${root}${path}`, 'utf-8'));

    if (!path.endsWith('.json')) logger.error('请检查配置文件格式，应该为json格式！');
    else if (!repoMapping) logger.error('请检查配置文件是否存在！');
    else {
      for (const adds in repoMapping) {
        if (existsSync(adds)) spawnSync('git', ['pull'], { stdio: 'inherit' })
        else spawnSync('git', ['clone', repoMapping[adds], adds], { stdio: 'inherit' })
      }
    }
  } catch (error) {
    logger.error(error);
  }
}